unit DMDownloads;

interface

uses
 SysUtils;

const
 //states
 dsPause = 0;
 dsPausing = 1;
 dsDownloaded = 2;
 dsDownloading = 3;
 dsError = 4;
 dsErroring = 5;
 dsQueue = 6;
 dsAll = $10;

 //units
 cUnitsLen = 6;
 cDefaultUnits = 0;

 //info tags
 sdiState = 'state';
 sdiURL = 'url';
 sdiResume = 'resumemode';
 sdiSaveTo = 'saveto';
 sdiSize = 'size';
 sdiDownloaded = 'downloadedsize';
 sdiSpeed = 'speed';
 sdiTime = 'time';
 sdiTimeLeft = 'timeleft';
 sdiDesc = 'description';
 sdiCategory = 'categoryid';
 sdiDate = 'date';
 sdiPriority = 'priority';
 sdiMirror1 = 'mirror1';
 sdiMirror2 = 'mirror2';
 sdiMirror3 = 'mirror3';
 sdiMirror4 = 'mirror4';
 sdiMirror5 = 'mirror5';

 //resume
 rsmNoResume = 1;
 rsmCanResume = 2;

 //priority
 prHigh = 1;
 prNormal = 2;
 prLow = 3;

var
 sdiDateFS: TFormatSettings;

function GetStateDesc(State: Integer; Picto: Boolean = false): String;
function UnitsText(Index: Integer; Speed: Boolean = false): String;
function FileFormatSize(Value: Int64; Units: Integer = 0; UseUnits: Boolean = true; Speed: Boolean = false): string;

implementation

uses
 gnugettext;

function GetStateDesc(State: Integer; Picto: Boolean = false): String;
begin
 case State of
  dsPause:
   begin
    if Picto then
      Result:='[||]'
    else
      Result:=_('Pause');
   end;
  dsDownloaded:
   begin
    if Picto then
      Result:='[==]'
    else
      Result:=_('Downloaded');
   end;
  dsDownloading:
   begin
    if Picto then
      Result:='[>>]'
    else
      Result:=_('Downloading');
   end;
  dsError:
   begin
    if Picto then
     Result:='[!!]'
    else
     Result:=_('Error');
   end;
  dsQueue:
   begin
    if Picto then
      Result:='[vv]'
    else
      Result:=_('Queue');
   end;
  dsAll: Result:=_('All downloads');
  else Result:='';
  end;
end;

function UnitsText(Index: Integer; Speed: Boolean = false): String;
begin
 case Index of
  0: Result:=_('Auto');
  1: Result:=_('bytes');
  2: Result:=_('KB');
  3: Result:=_('MB');
  4: Result:=_('GB');
  5: Result:=_('TB');
  else
    Result:='';
  end;
 if Speed then
  begin
   if (Index<>0) and (Result<>'') then
     Result:=Result+'/'+_('sec');
  end;
end;

function FileFormatSize(Value: Int64; Units: Integer = 0; UseUnits: Boolean = true; Speed: Boolean = false): string;
var
  SizeUnit: Integer;
  Temp: Int64;
  UseUnit: Integer;
begin
  if (Units<0) or (Units>cUnitsLen-1) then
    UseUnit:=0
  else
    UseUnit:=Units;
  SizeUnit:=1;
  if UseUnit=1 then
    Result:=IntToStr(Value)
  else
   begin
    Temp:=1;
    if UseUnit=0 then
     begin
      while (Temp<Value) and (SizeUnit<cUnitsLen-1) do
       begin
        Temp:=Temp shl 10;
        Inc(SizeUnit);
       end;
      if (Temp>1) and (Temp>Value) then
       begin
        Temp:=Temp shr 10;
        Dec(SizeUnit);
       end;
     end
    else
     begin
      while SizeUnit<UseUnit do
       begin
        Temp := Temp shl 10;
        Inc(SizeUnit);
       end;
     end;
    if SizeUnit>1 then
      Result:=Format('%*.3f', [4, Value/Temp])
    else
      Result:=IntToStr(Value);
   end;
  if UseUnits then
    Result := Result + UnitsText(SizeUnit, Speed);
end;

initialization

  GetLocaleFormatSettings($0409, sdiDateFS);

end.

